layui.define(["form","jquery"],function(exports){
    var form = layui.form,
    $ = layui.jquery,
    Address = function(province_a,city_b,area_c){};

    Address.prototype.provinces = function(province_a,city_b,area_c) {
    	var provinces_a=province_a
    	var provinces_b=city_b
    	var provinces_c=area_c
        //加载省数据
        var proHtml = '',that = this;
        $.get("js/assets/address.json", function (data) {
            for (var i = 0; i < data.length; i++) {
                proHtml += '<option value="' + data[i].code + '">' + data[i].name + '</option>';
            }
            //初始化省数据
            $('select[name='+province_a+']').append(proHtml);
//            $("#"+province_a).append(proHtml);
            form.render();
            form.on('select('+province_a+')', function (proData) {
                $('select[name='+area_c+']').html('<option value="">请选择县/区</option>');
//            	  $("#"+area_c).html('<option value="">请选择县/区</option>');
                var value = proData.value;
                if (value > 0) {
                    that.citys(data[$(this).index() - 1].childs,provinces_a,provinces_b,provinces_c);
                } else {
//                    $('"select[name='+city_b+']"').attr("disabled", "disabled");
                	   $("#"+city_b).attr("disabled", "disabled");
                }
            });
        })
    }

    //加载市数据
    Address.prototype.citys = function(citys,province_a,city_b,area_c) {
        var cityHtml = '<option value="">请选择市</option>',that = this;
        for (var i = 0; i < citys.length; i++) {
            cityHtml += '<option value="' + citys[i].code + '">' + citys[i].name + '</option>';
        }
        $('select[name='+city_b+']').html(cityHtml).removeAttr("disabled");
//        $("#"+city_b).html(cityHtml).removeAttr("disabled");
        form.render();
        form.on('select('+city_b+')', function (cityData) {
            var value = cityData.value;
            if (value > 0) {
                that.areas(citys[$(this).index() - 1].childs,province_a,city_b,area_c);
            } else {
                $('select[name='+area_c+']').attr("disabled", "disabled");
//                $("#"+area_c).attr("disabled", "disabled");
            }
        });
    }

    //加载县/区数据
    Address.prototype.areas = function(areas,province_a,city_b,area_c) {
        var areaHtml = '<option value="">请选择县/区</option>';
        for (var i = 0; i < areas.length; i++) {
            areaHtml += '<option value="' + areas[i].code + '">' + areas[i].name + '</option>';
        }
        $('select[name='+area_c+']').html(areaHtml).removeAttr("disabled");
//        $("#"+area_c).html(areaHtml).removeAttr("disabled");
        form.render();
    }

    var address = new Address();
    exports("address",function(province_a,city_b,area_c){
        address.provinces(province_a,city_b,area_c);
    });
})